package com.ruoyi.web.controller.common;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.service.impl.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/video")
public class VideoEncryptController {

    @Autowired
    private VideoService videoService;

    // 生成密钥（仅管理员可调用）
    @GetMapping("/generate-key")
    public ResponseEntity<String> generateKey() {
        String key = videoService.generateVideoKey();
        return ResponseEntity.ok("AES-256 密钥（请妥善保存）：" + key);
    }

    // 加密视频（接收上传的视频，返回加密后的文件下载链接）
    @PostMapping("/encrypt")
    public AjaxResult encryptVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam("key") String base64Key) throws IOException {
        // 临时保存上传的原始视频
        String originalPath = RuoYiConfig.getPathVideoOriginal() + UUID.randomUUID() + ".mp4";
        file.transferTo(new File(originalPath));

        // 加密后的视频路径
        String encryptedPath = RuoYiConfig.getPathVideo() + UUID.randomUUID() + ".mp4";

        // 加密（AAD 简化为文件名，实际应存储 AAD 供解密使用）
        String aad = videoService.encryptVideo(originalPath, encryptedPath, base64Key);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("aad",aad);
        ajaxResult.put("path",encryptedPath);

        // 返回加密文件下载链接（实际项目需配置静态资源访问或文件服务器）
        return ajaxResult;
    }

    // 解密视频（接收加密视频和 AAD，返回解密后的文件）
    @PostMapping("/decrypt")
    public ResponseEntity<String> decryptVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam("key") String base64Key,
            @RequestParam("aad") String aad) throws IOException {
        // 临时保存上传的加密视频
        String encryptedPath = RuoYiConfig.getPathVideoOriginal() + UUID.randomUUID() + ".mp4";
        file.transferTo(new File(encryptedPath));

        // 解密后的视频路径
        String decryptedPath = RuoYiConfig.getPathVideo() + UUID.randomUUID() + ".mp4";

        // 解密
        videoService.decryptVideo(encryptedPath, decryptedPath, base64Key, aad);

        return ResponseEntity.ok("解密成功，下载链接：/download?file=" + decryptedPath);
    }

    // 解密视频（接收加密视频和 AAD，返回解密后的文件）
    @PostMapping("/decryptOnline")
    public AjaxResult decryptVideo(
            @RequestParam("key") String base64Key,
            @RequestParam("path") String path,
            @RequestParam("aad") String aad) throws IOException {

        String encryptedPath = RuoYiConfig.getPathVideo() + path;

        // 解密后的视频路径
        String fileName = UUID.randomUUID() + ".mp4";
        String decryptedPath = RuoYiConfig.getPathVideoOriginal() + fileName;

        // 解密
        videoService.decryptVideo(encryptedPath, decryptedPath, base64Key, aad);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("play","study/original/" + fileName);
        return ajaxResult;
    }
}
